﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaoYL.BoosterStation
{
    public partial class FrmUserManage : Form
    {
        #region fields
        private SysAdminService sysAdminService = new SysAdminService();
        private List<SysAdmin> sysAdmins = new List<SysAdmin>();
        #endregion

        #region Ctor
        public FrmUserManage()
        {
            InitializeComponent();
            this.dgv_User.AutoGenerateColumns = false;
            this.dgv_User.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.Load += FrmUserManage_Load;
        }
        #endregion

        #region Methods
        private void FrmUserManage_Load(object sender, EventArgs e)
        {
            this.cmb_RoleName.DataSource = Enum.GetValues(typeof(RoleName));
            RefreshUserList();

            if (sysAdmins.Count>0)
            {
                UpdateUserInfo(sysAdmins.First());
            }
        }
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="sysAdmin"></param>
        private void UpdateUserInfo(SysAdmin sysAdmin)
        {
           this.txt_LoginName.Text = sysAdmin.LoginName;
            this.txt_LoginPwd.Text = sysAdmin.LoginPwd;
            this.cmb_RoleName.SelectedItem = sysAdmin.RoleName;
        }

        /// <summary>
        /// 刷新用户列表
        /// </summary>
        private void RefreshUserList()
        {
            sysAdmins = sysAdminService.QuerySysAdmins();

            if(sysAdmins.Count > 0)
            {
                this.dgv_User.DataSource = null;
                this.dgv_User.DataSource = sysAdmins;
            }
            else
            {
                this.dgv_User.DataSource = null;
            }
        }


        #region 无边框拖动 

        private Point mPoint;
        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }
        #endregion
        /// <summary>
        /// DGV插入行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_User_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewHelper.DgvRowPostPaint(dgv_User, e); 
        }

        private void label_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_AddUser_Click(object sender, EventArgs e)
        {
            if(txt_LoginName.Text.Trim().Length==0 || txt_LoginPwd.Text.Trim().Length == 0 ||cmb_RoleName.Text.Trim().Length==0)
            {
               new MsgBoxConfirm("新增用户","用户名或密码不能为空").ShowDialog();
               return;
            }

            //2.判断是否存在
            if (sysAdmins.Any(u => u.LoginName == txt_LoginName.Text.Trim()))
            {
                new MsgBoxConfirm("新增用户", "用户名已存在").ShowDialog();
                return;
            }

            //3.封装对象
            SysAdmin sysAdmin = new SysAdmin()
            {
                LoginName = txt_LoginName.Text.Trim(),
                LoginPwd = txt_LoginPwd.Text.Trim(),
                RoleName = (RoleName)Enum.Parse(typeof(RoleName), cmb_RoleName.Text.Trim())
            };
            //4.添加用户
            if(sysAdminService.AddSysAdmin(sysAdmin))
            {
                new MsgBoxConfirm("新增用户", "添加成功").ShowDialog();
                //5.刷新列表
                RefreshUserList();
            }
            else
            {
                new MsgBoxConfirm("新增用户", "添加失败,请检查!").ShowDialog();
            }
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ModifyUser_Click(object sender, EventArgs e)
        {
            if (txt_LoginName.Text.Trim().Length == 0 || txt_LoginPwd.Text.Trim().Length == 0 || cmb_RoleName.Text.Trim().Length == 0)
            {
                new MsgBoxConfirm("修改用户", "用户名或密码不能为空").ShowDialog();
                return;
            }

            

            //2.封装对象
            SysAdmin sysAdmin = new SysAdmin()
            {
                LoginId = sysAdmins[dgv_User.SelectedRows[0].Index].LoginId,
                LoginName = txt_LoginName.Text.Trim(),
                LoginPwd = txt_LoginPwd.Text.Trim(),
                RoleName = (RoleName)Enum.Parse(typeof(RoleName), cmb_RoleName.Text.Trim())
            };
            //4.添加用户
            if (sysAdminService.ModifySysAdmin(sysAdmin))
            {
                //5.刷新列表
                RefreshUserList();
            }
            else
            {
                new MsgBoxConfirm("修改用户", "添加失败,请检查!").ShowDialog();
            }
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DeleteUser_Click(object sender, EventArgs e)
        {
            if (dgv_User.SelectedRows.Count > 0)
            {
                //int loginId=Convert.ToInt32(dgv_User.SelectedRows[0].Cells["LoginId"].Value);
                int loginId = sysAdmins[dgv_User.SelectedRows[0].Index].LoginId;

                if (sysAdminService.DeleteSysAdmin(loginId))
                {
                    //5.刷新列表
                    RefreshUserList();
                }
                else
                {
                    new MsgBoxConfirm("删除用户", "添加失败,请检查!").ShowDialog();
                }
            }

            
        }
        /// <summary>
        /// 单击用户列表，更新用户信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_User_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex>0)
            {
                UpdateUserInfo(sysAdmins[e.RowIndex]);
            }
        }

        #endregion
    }



}
